package com.czp.audit.service.impl;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.ibatis.session.RowBounds;

import com.czp.audit.mapper.IAuditMapper;
import com.czp.audit.model.AuditProcess;
import com.czp.audit.model.AuditType;
import com.czp.audit.service.IAuditService;
import com.czp.framework.model.DataStoreTemplate;
import com.czp.framework.util.DateUtils;
import com.google.gson.Gson;

/**
 * 【环 境】：J2SE 1.7
 * @author   柴志鹏	CHAIZP@GMAIL.COM
 * @version  version 1.0
 * @since    2014-11-13
 */
public class AuditServiceImpl implements IAuditService {
	private IAuditMapper auditMapper;
	Gson gson = new Gson();

	public String queryAuditTypeList(AuditType at) throws Exception {
		RowBounds rb = new RowBounds(at.getStart(), at.getLimit());
		DataStoreTemplate dst = new DataStoreTemplate();
		dst.setList(auditMapper.queryAuditTypeList(at, rb));
		dst.setTotal(auditMapper.queryAuditTypeCount(at));
		return gson.toJson(dst);
	}
	
	public String queryAuditProcessByTypeId(String typeId, int start, int limit) throws Exception {
		RowBounds rb = new RowBounds(start, limit);
		List<AuditProcess> ap = auditMapper.queryAuditProcessListByTypeId(typeId, rb);
		DataStoreTemplate dst = new DataStoreTemplate();
		dst.setList(ap);
		dst.setTotal(ap.size());
		return gson.toJson(dst);
	}
	
	public int sortProcess(String json) throws Exception {
		JSONArray jsonArr = JSONArray.fromObject(json);
		if(jsonArr == null || jsonArr.isEmpty()) {
			return 0;
		}
		AuditProcess ap = new AuditProcess();
		int i;
		for(i = 0; i < jsonArr.size(); i++) {
			JSONObject jsonObj = jsonArr.getJSONObject(i);
			ap.setId(jsonObj.getString("id"));
			ap.setAuditProcess(String.valueOf(i+1));
			auditMapper.sortProcess(ap);
		}
		return i;
	}
	
	public int auditTypeController(AuditType at) throws Exception {
		at.setModifyDate(DateUtils.getSysTimestamp());
		if(at.getCudType().equals("C")) {//添加
			return auditMapper.insertAuditType(at);
		} else if(at.getCudType().equals("U")) {//修改
			return auditMapper.updateAuditType(at);
		} else if(at.getCudType().equals("D")) {//删除
			return auditMapper.deleteAuditType(at);
		}
		return 0;
	}
	
	public String getOptionallyProcess(String typeId) throws Exception {
		return gson.toJson(auditMapper.getOptionallyProcess(typeId));
	}
	
	public String getSelectedProcess(String typeId) throws Exception {
		return gson.toJson(auditMapper.getSelectedProcess(typeId));
	}
	
	public int auditProcessController(String typeId, String data) throws Exception {
		if(typeId != null && !typeId.equals("")) {
			JSONArray jsonArr = JSONArray.fromObject(data);
			if(jsonArr == null || jsonArr.isEmpty()) {
				//如果是删除全部流程
				auditMapper.deleteAuditProcessByTypeId(typeId);
				return 0;
			}
			// 查询当前已选岗位的数量
			int selectedCount = auditMapper.getSelectedCountByTypeId(typeId);
			// 获取被删除的岗位数量
			int deleteCount = auditMapper.deleteAuditProcessByTypeId(typeId);
			if(selectedCount == deleteCount) {
				AuditProcess ap = new AuditProcess();
				int i = 0;
				for (int cnt = jsonArr.size(); i < cnt; i++) {
					JSONObject jsonObj = jsonArr.getJSONObject(i);
					ap.setTypeId(typeId);
					ap.setJobId(jsonObj.getString("jobId"));
					ap.setAuditProcess(String.valueOf(i + 1));
					auditMapper.addSelectedJob(ap);
				}
				return i;
			}
		}
		return 0;
	}

	/**
	 * 获取 auditMapper
	 * @return 返回 auditMapper
	 */
	public IAuditMapper getAuditMapper() {
		return auditMapper;
	}

	/**
	 * 设置 auditMapper
	 * @param 对auditMapper进行赋值
	 */
	public void setAuditMapper(IAuditMapper auditMapper) {
		this.auditMapper = auditMapper;
	}
	
	public String testProcess(String id) {
		String json = gson.toJson(auditMapper.testProcess(id));
		System.out.println(json);
		return json;
	}

}
